home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / usenet / sources / volume91 / utilitys / sman_1_1 / part01 / src / sless.c < prev    next >
C/C++ Source or Header  |  1991-07-29  |  6KB  |  360 lines

  1.  
  2. #include "sless_defs.h"
  3. #include <libraries/sregexpbase.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <ctype.h>
  7. #include <clib/dos_protos.h>
  8.  
  9. Prototype void            usage(void);
  10. Prototype void            refreshscreen(void);
  11. Prototype void            waitcommand(void);
  12. Prototype void            leave(int, char *);
  13. Prototype int            putline(void);
  14.  
  15. const char memory[] = "sless: Unable to allocate memory.\n";
  16. const char end[]    = " \x9b7m-- END OF FILE --\x9b0m";
  17. const char top[]    = " \x9b7m-- TOP OF FILE --\x9b0m";
  18. const char nopat[]  = " \x9b7m-- Pattern not found --\x9b0m";
  19. const char nolast[] = " \x9b7m-- No previous pattern --\x9b0m";
  20. const char wait[]   = " \x9b7m-- Searching --\x9b0m";
  21. const char abort[]  = " \x9b7m-- Search aborted --\x9b0m";
  22.  
  23. char seek = 0;
  24. long startpos = 0,nextpos = 0;
  25. FILE *file = NULL,*ofile = NULL;
  26. BPTR con = 0;
  27. int sx,sy;
  28. struct SregExp *lastspat = NULL;
  29.  
  30. int main(ac,av)
  31. int ac;
  32. char **av;
  33. {
  34.     int line = 0,i;
  35.     char *p,*oldwin = NULL;
  36.  
  37.     show_line;
  38.     while (ac > 0) {
  39.     ac--;
  40.     av++;
  41.     if (**av != '-')
  42.         break;
  43.     switch (*(*av+1)) {
  44.         case ('l') :
  45.         if (line != 0)
  46.             usage();
  47.         p = *av+2;
  48.         while (isdigit(*p))
  49.             line = line*10 + *p++ - '0';
  50.         break;
  51.         case ('c') :
  52.         if (line != 0)
  53.             usage();
  54.         p = *av+2;
  55.         while (isdigit(*p))
  56.             line = line*10 - *p++ + '0';
  57.         break;
  58.         default :
  59.         usage();
  60.     }
  61.     }
  62.  
  63.     if (ac < 1)
  64.     usage();
  65.     if (!(file = sopen(*av)))
  66.     leave(15,"Unable to open input stream.\n");
  67.  
  68.     /* try and find the length of a file.  If it fails or is zero, then
  69.        we assume we cannot seek into the file.    This catches trying to
  70.        seek into pipes. */
  71.     sseek(file,0,SEEK_END);
  72.     if (stell(file) <= 0)
  73.     seek = 0;
  74.     else {
  75.     seek = 1;
  76.     sseek(file,0,SEEK_SET);
  77.     }
  78.  
  79.     con = makeoutput(*av);
  80.  
  81.     if (line < 0) {
  82.     startpos = -line;
  83.     if (seek)
  84.         sseek(file,startpos,SEEK_SET);
  85.     else
  86.         for (i = 0; i > line; i--)
  87.         getc(file);
  88.     } else {
  89.     for (i = 0; i < line && (p = sgetline(file)); i++) {
  90.         if (p[strlen(p)-1] == 12)
  91.         i--;
  92.         show_line;
  93.         free(p);
  94.         show_line;
  95.         if (seek)
  96.         startpos = stell(file);
  97.     }
  98.     }
  99.  
  100.     findsize(&sx,&sy,con);
  101.     refreshscreen();
  102.     waitcommand();
  103.  
  104.     closeoutput(con);
  105.     con = 0;
  106.     sclose(file);
  107.     file = NULL;
  108.     return 0;
  109. }
  110.  
  111. void
  112. usage()
  113. {
  114.     show_line;
  115.     sputs("sless V"\
  116.       VERSION\
  117.       ", "\
  118.       __DATE__\
  119.       ", \xA9Copyright 1991 by Jon Spencer.\n");
  120.     sputs("Usage: sless [-llineno | -ccharpos] file\n");
  121.     sputs("Where lineno is the optional starting line number,\n");
  122.     sputs("or charpos is the optional starting character positon.\n");
  123.     exit(2);
  124. }
  125.  
  126. void
  127. leave(x,p)
  128. int x;
  129. char *p;
  130. {
  131.     show_line;
  132.     sputs(p);
  133.     closeoutput(con);
  134.     if (con)
  135.     closeoutput(con);
  136.     if (file)
  137.     sclose(file);
  138.     if (ofile)
  139.     sclose(ofile);
  140.     exit(x);
  141. }
  142.  
  143. void
  144. waitcommand()
  145. {
  146.     short c;
  147.  
  148.     show_line;
  149.     do {
  150.     switch (c = getconchar(con)) {
  151.         case (' ') :
  152.         sgetcur(file);
  153.         if (seof(file) && nextpos > 0)
  154.             nextpos = -1;
  155.         if (nextpos >= 0) {
  156.             startpos = nextpos;
  157.             refreshscreen();
  158.         } else {
  159.             bottomline();
  160.             if (nextpos-- == -2)
  161.             return;
  162.             putconstr(end,con);
  163.         }
  164.         break;
  165.         case ('<') :
  166.         case (makeconid('T',0)) :
  167.         if (seek) {
  168.             if (startpos == 0) {
  169.             bottomline();
  170.             putconstr(top,con);
  171.             } else {
  172.             startpos = 0;
  173.             refreshscreen();
  174.             }
  175.         }
  176.         break;
  177.         case (13) :
  178.         case (makeconid('B',0)) :
  179.         sgetcur(file);
  180.         if (seof(file) && nextpos > 0)
  181.             nextpos = -1;
  182.         if (nextpos >= 0) {
  183.             downone();
  184.         } else {
  185.             bottomline();
  186.             if (nextpos-- == -2)
  187.             return;
  188.             putconstr(end,con);
  189.         }
  190.         break;
  191.         case ('q') :
  192.         bottomline();
  193.         return;
  194.         case (8) :
  195.         case ('b') :
  196.         if (seek) {
  197.             if (startpos == 0) {
  198.             bottomline();
  199.             putconstr(top,con);
  200.             } else
  201.             backup(sy-2);
  202.         }
  203.         break;
  204.         case ('d') :
  205.         sgetcur(file);
  206.         if (seof(file) && nextpos > 0)
  207.             nextpos = -1;
  208.         if (nextpos >= 0) {
  209.             int i = 1;
  210.  
  211.             while (i++ < sy/2 && nextpos > 0)
  212.             startpos = skiponeline(startpos);
  213.             refreshscreen();
  214.         } else {
  215.             bottomline();
  216.             if (nextpos-- == -2)
  217.             return;
  218.             putconstr(end,con);
  219.         }
  220.         break;
  221.         case ('>') :
  222.         case (makeconid('S',0)) :
  223.         if (seek) {
  224.             sseek(file,-1,SEEK_END);
  225.             startpos = stell(file);
  226.             backup(sy-1);
  227.         }
  228.         break;
  229.         case ('u') :
  230.         if (seek) {
  231.             if (startpos != 0) {
  232.             backup(sy/2-1);
  233.             } else {
  234.             bottomline();
  235.             putconstr(top,con);
  236.             }
  237.         }
  238.         break;
  239.         case (makeconid('A',0)) :
  240.         if (seek) {
  241.             if (startpos != 0) {
  242.             uponeline();
  243.             } else {
  244.             bottomline();
  245.             putconstr(top,con);
  246.             }
  247.         }
  248.         break;
  249.         case ('/') :
  250.         if (seek) {
  251.             bottomline();
  252.             putconstr("/",con);
  253.             if (!searchfor())
  254.             break;
  255.             nextpat();
  256.         }
  257.         break;
  258.         case ('n') :
  259.         if (seek)
  260.             nextpat();
  261.         break;
  262.         case ('p') :
  263.         if (seek)
  264.             prevpat();
  265.         break;
  266.         case (';') :
  267.         if (seek) {
  268.             bottomline();
  269.             putconstr(";",con);
  270.             if (!searchfor())
  271.             break;
  272.             prevpat();
  273.         }
  274.         break;
  275.         case (makeconid('?','~')) :
  276.         case ('h') :
  277.         case ('?') :
  278.         if (!ofile)
  279.             behelpfull();
  280.         break;
  281.     }
  282.     } while (TRUE);
  283. }
  284.  
  285. void
  286. refreshscreen()
  287. {
  288.     int y,c;
  289.     long t;
  290.  
  291.     show_line;
  292.     putconstr("\x9b0 p\x0c",con);
  293.  
  294.     y = sy;
  295.     if (seek)
  296.     sseek(file,startpos,SEEK_SET);
  297.     do {
  298.     if (seek && y > 1)
  299.         nextpos = stell(file);
  300.     } while (--y > 0 && !(c = putline()));
  301.  
  302.     if (seof(file))
  303.     nextpos = -1;
  304.     if (c == 2) {
  305.     nextpos = stell(file);
  306.     }
  307.  
  308.     while (--y > 0)
  309.     putconchar(10,con);
  310.  
  311.     putconstr(": \x9b p",con);
  312. }
  313.  
  314. int
  315. putline()
  316. {
  317.     char *p,*q;
  318.  
  319.     show_line;
  320.  
  321.     if (!(p = sgetline(file))) {
  322.     putconchar(10,con);
  323.     return TRUE;
  324.     }
  325.     q = skipdispline(p);
  326.     if (*(q-1) == 12) {
  327.     *(q-1) = 0;
  328.     putconstr(p,con);
  329.     putconstr("^L\n",con);
  330.     free(p);
  331.     return 2;
  332.     } else if (*q) {
  333.     sseek(file,q - p - strlen(p),SEEK_CUR);
  334.     *q++ = '\n';
  335.     *q = 0;
  336.     }
  337.     putconstr(p,con);
  338.     free(p);
  339.     return seof(file);
  340. }
  341.  
  342. /*
  343. #include <stdarg.h>
  344.  
  345. Prototype int printf(const char *, ...);
  346. extern int    vsprintf(char *, const char *, va_list);
  347.  
  348. int printf(f, ...)
  349. const char *f;
  350. {
  351.     char buff[100];
  352.     va_list va;
  353.  
  354.     va_start(va,f);
  355.     vsprintf(buff,f,va);
  356.     va_end(va);
  357.     Write(Output(),buff,strlen(buff));
  358. }
  359. */
  360.